<?php
/**
 * search.php 搜索
 * Jack在首页输入要查询的单词后，系统将跳转到该页面给Jack呈现找到的单词的释义。
 * Jack可以在该页面继续搜索其他的单词。
 */
require_once 'setting.php';
require_once 'smarty.php';

require_once 'functions.php';

require_once INCLUDESPATH . 'dict/IcibaDict.php';
require_once INCLUDESPATH . 'dict/DictcnDict.php';

require_once INCLUDESPATH . 'db/Word.php';

# start the session function
if (!isset($_SESSION)) {
	session_start();
}

# << start - 搜索单词并呈现单词的释义
if (isset($_GET['word'])) {
	# 清楚单词两侧的空白符
	$word = trim($_GET['word']);
	# 判断word是否满足特定的格式
	if ($word == '') {
		showSearchPage($word, NULL, 'Please enter a word.');
		exit();
	} elseif (!validWord($word)) {
		showSearchPage($word, NULL, 'Invalid word. Please check your input.');
		exit();
	} else {
		$results = lookup($word);
		showSearchPage($word, $results);
		exit();
	}
}
# 如果Jack直接进入该链接且没有参数word，跳转到首页
else {
	jumpToPage('index.php');
}
# >> end - 搜索单词并呈现单词的释义


/**
 * 显示52danci的单词搜索结果的页面给Jack
 */
function showSearchPage($word, $results, $msg = '') {
	global $pagetitle, $tipsmsg, $dictsearchlinks;
	$smarty = new MySmarty();

	# 页面标题和输入框中的单词
	if ($word != '') {
		$smarty->assign('title', $word . ' - ' . $pagetitle['search']);
		$smarty->assign("word", $word);
	} else {
		$smarty->assign('title', $pagetitle['search']);
	}
	# 页面提示信息
	if ($msg != '') {
		$smarty->assign('tipsmsg', $msg);
	}
	# 发音提示信息 pronunciation tips
	$smarty->assign('prontips', $tipsmsg['prontips']);
	$smarty->assign('dictsearchlinks', $dictsearchlinks);
	# 单词的释义
	# @todo 此如应当加以处理，让单词呈现的方式更加的合理
	if (!empty($results)) {
		$smarty->assign("results", $results);
	}
	$smarty->display('search.tpl');
}
?>